/* Licence:
*   Use this however/wherever you like, just don't blame me if it breaks anything.
*
* Credit:
*   If you're nice, you'll leave this bit:
*
*   Class by Pierre-Alexandre Losson -- http://www.telio.be/blog
*   email : plosson@users.sourceforge.net
*/

/*
*  Changed for Part 2, by Ken Cochrane
*  Changed for Part 3, by kof4ever
*  Modification : add upload status
*/
function refreshProgress()
{
	//send callback
    UploadMonitor.getUploadInfo(updateProgress);
    
}

function updateProgress(uploadInfo)
{
	var status = uploadInfo.status;
	
	updateStatusMessage(status);
	
    if (uploadInfo.inProgress)
    {
        var fileIndex = uploadInfo.fileIndex;
		var	fileName = getNameOfFileIndex(fileIndex-1);
		var totalNumberOfFiles = getNumberOfFiles();
		var totalSize = uploadInfo.totalSize;
		var bytesRead = uploadInfo.bytesRead;
		// milliseconds
		var elapsedTime = uploadInfo.elapsedTime;
		// Approx. upload speed(kbytes/sec)
		var speed = parseFloat((bytesRead/elapsedTime)*1000/1024);
		//required time for uploding files(sec)
		var totalTime = totalSize/(speed*1000);
		
        var progressPercent = Math.ceil((bytesRead / totalSize) * 100);

        document.getElementById('progressBarText').innerHTML = 'upload in progress: ' + progressPercent + '%';

        document.getElementById('progressBarBoxContent').style.width = parseInt(progressPercent * 3.5) + 'px';

//-----------------------------------start of upload status ---------------------------------------//
		document.getElementById('fileName').innerHTML = "File Uploading : "+fileName;

		document.getElementById('files').innerHTML =  fileIndex + " of "+totalNumberOfFiles;
		
		document.getElementById('current').innerHTML = convertSize(bytesRead) + " / "+convertSize(totalSize);
		
		document.getElementById('speed').innerHTML = setFixedPoint(speed,2) + " kbytes/sec";
		
		document.getElementById('time').innerHTML = convertSeconds(parseInt(elapsedTime/1000));
		
		document.getElementById('left').innerHTML = convertSeconds(parseInt(totalTime-elapsedTime/1000));
		
//-----------------------------------end of upload status ---------------------------------------//
        window.setTimeout('refreshProgress()', 1000);
    }else{
    	// status="done" , "error"
    	hideProgressBar();
    	if(status=="done")
    		document.getElementById('updateStatusMsg').innerHTML = "copying files from temp dir to upload dir... <img src='image/bigrotation2.gif'/>"	
    	
    }
	
    return true;
}

function startProgress()
{
    updateStatusMessage("");
    document.getElementById('progressBar').style.visibility = 'visible';
    document.getElementById('status').style.visibility = 'visible';
    document.getElementById('progressBarText').innerHTML = 'upload in progress: 0%';

    // wait a little while to make sure the upload has started ..
    window.setTimeout("refreshProgress()", 1500);
    return true;
}

/* added by kof4ever
 * handling an interruption by user
 */
 
 function stopProgress(){
 	alert("Upload process is stopped by User");
 	// close a current progressBar window.
 	window.close();
 }


function hideProgressBar()
{
    document.getElementById('progressBar').style.visibility = 'hidden';
    document.getElementById('status').style.visibility = 'hidden';
    document.getElementById('progressBarText').innerHTML = '';
    
}
// UploadMonitor.js (generated by DWR)
function UploadMonitor() { 
      UploadMonitor._path = '';
      UploadMonitor.getUploadInfo = function(callback) {
	      DWREngine._execute(UploadMonitor._path, 'UploadMonitor', 'getUploadInfo', callback);
      }
}

